package kr.hkit.powerjava.chapter17;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.SwingConstants;
import javax.swing.border.Border;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RadioButtonSwingCom extends JPanel implements ActionListener {
	private JPanel panelSize;
	private JLabel lblTop;
	private JRadioButton small;
	private JRadioButton medium;
	private JRadioButton large;
	private JLabel lblResult;

	/**
	 * Create the panel.
	 */
	public RadioButtonSwingCom() {

		initialize();
	}
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		lblTop = new JLabel("어떤 크기의 커피를 주문하시겠습니까?");
		lblTop.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblTop, BorderLayout.NORTH);
		
		panelSize = new JPanel();
		add(panelSize);
		
		Border border = BorderFactory.createTitledBorder("크기");
		panelSize.setBorder(border);
		
		small = new JRadioButton("Small");
		small.addActionListener(this);
		medium = new JRadioButton("Medium");
		medium.addActionListener(this);
		large = new JRadioButton("Large");
		large.addActionListener(this);
		
		ButtonGroup size = new ButtonGroup();
		size.add(small);
		size.add(medium);
		size.add(large);
		
		panelSize.add(small);		
		panelSize.add(medium);		
		panelSize.add(large);
		
		lblResult = new JLabel("크기가 선택되지 않았습니다.");
		lblResult.setHorizontalAlignment(SwingConstants.CENTER);
		lblResult.setForeground(Color.red);
		add(lblResult, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == large) {
			lblResult.setText("Large 크기가 선택되었습니다.");
		}
		if (e.getSource() == medium) {
			lblResult.setText("Medium 크기가 선택되었습니다.");
		}
		if (e.getSource() == small) {
			lblResult.setText("Small 크기가 선택되었습니다.");
		}
	}
}
